package com.btex.app.tools;

import android.content.Context;

import com.btex.app.Config;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.apache.http.impl.cookie.BasicClientCookie;

/**
 * Created by wcx on 16/3/24.
 */
public class CookieTool {

    private static PreferencesCookieStore cookieStore;

    public static PreferencesCookieStore getCookieStore(Context cxt, SharedPreferencesUtil sp) {
        if (cxt != null) {

            cookieStore = new PreferencesCookieStore(cxt);

            String token = sp.getString(Config.TOKEN, null);
            if (token != null) {

                BasicClientCookie cookie = new BasicClientCookie(Config.TOKEN, token);
                cookie.setDomain(Config.DOMAIN);
                cookie.setPath("/");

                cookieStore.addCookie(cookie);

            } else {

                String cookies = sp.getString(Config.COOKIE, null);

                BasicClientCookie cookie = new BasicClientCookie(Config.TOKEN, cookies);
                cookie.setDomain(Config.DOMAIN);
                cookie.setPath("/");

                cookieStore.addCookie(cookie);
            }

        }
        return cookieStore;
    }

}
